package org.wyn.action;


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.wyn.common.ResultType;
import org.wyn.mongodb.bean.User;
import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.dao.impl.UserDetailDao;

import com.google.inject.Inject;
/**
 * 用户详细资料action
 * @author wyn
 *
 */
@Namespace("/user_detail")
@ParentPackage("security")
public class UserDetailAction extends WynActionSupport{
	/**
	 * 自己的资料页面
	 * @return
	 */
	@Action(value="getUserDetail",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "user_detail.ftl")})
	public String getUserDetail(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return SUCCESS;
	}
	/**
	 * 别人的资料页面
	 * @return
	 */
	@Action(value="go_other_user_detail",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "other_user_detail.ftl")})
	public String goOtherUserDetail(){
		ud = userDetailDao.findOne("userName", otherUserName);
		return SUCCESS;
	}
	/**
	 *
	 * 基本信息
	 */
	/**
	 * 跳转到基本信息修改页面
	 * @return
	 */
	@Action(value="basic_info_edit_input",
			results = {@Result(name = INPUT, type = ResultType.FREEMARKER, location = "basic_info_edit.ftl")})
	public String basicInfoEditInput(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return INPUT;
	}
	//基本信息保存修改
	@Action(value="basic_info_save",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "basic_info_show.ftl")})
	public String basicInfoSave(){
		User user = getLoginUser();
		UserDetail udFromDB = userDetailDao.findOne("userName", user.getUserName());
		udFromDB.setGender(ud.getGender());
		udFromDB.setHomeProvince(ud.getHomeProvince());
		udFromDB.setHomeCity(ud.getHomeCity());
		udFromDB.setBirthYear(ud.getBirthYear());
		udFromDB.setBirthMonth(ud.getBirthMonth());
		udFromDB.setBirthDay(ud.getBirthDay());
		userDetailDao.save(udFromDB);//根据表单参数修改信息后保存
		ud = udFromDB;//ud 指向 udFromDB用于显示页面
		return SUCCESS;
	}
	//基本信息取消修改
	@Action(value="basic_info_cancel",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "basic_info_show.ftl")})
	public String basicInfoCancel(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return SUCCESS;
	}
	/**
	 * 学校信息
	 */
	@Action(value="education_info_edit_input",
			results = {@Result(name = INPUT, type = ResultType.FREEMARKER, location = "education_info_edit.ftl")})
	public String educationInfoEditInput(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return INPUT;
	}
	@Action(value="education_info_save",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "education_info_show.ftl")})
	public String educationInfoSave(){
		User user = getLoginUser();
		UserDetail udFromDB = userDetailDao.findOne("userName", user.getUserName());
		udFromDB.setUniversity(ud.getUniversity());
		udFromDB.setUniversityDepartment(ud.getUniversityDepartment());
		udFromDB.setUniversityYear(ud.getUniversityYear());
		udFromDB.setHighSchool(ud.getHighSchool());
		udFromDB.setHighSchoolYear(ud.getHighSchoolYear());
		udFromDB.setJuniorSchool(ud.getJuniorSchool());
		udFromDB.setJuniorSchoolYear(ud.getJuniorSchoolYear());
		udFromDB.setPrimarySchool(ud.getPrimarySchool());
		udFromDB.setPrimarySchoolYear(ud.getPrimarySchoolYear());
		userDetailDao.save(udFromDB);//根据表单参数修改信息后保存
		ud = udFromDB;//ud 指向 udFromDB用于显示页面
		return SUCCESS;
	}
	@Action(value="education_info_cancel",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "education_info_show.ftl")})
	public String educationInfoCancel(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return SUCCESS;
	}
	/**
	 *个人信息 
	 */
	@Action(value="personal_info_edit_input",
			results = {@Result(name = INPUT, type = ResultType.FREEMARKER, location = "personal_info_edit.ftl")})
	public String personalInfoEditInput(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return INPUT;
	}
	@Action(value="personal_info_save",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "personal_info_show.ftl")})
	public String personalInfoSave(){
		User user = getLoginUser();
		UserDetail udFromDB = userDetailDao.findOne("userName", user.getUserName());
		udFromDB.setHobby(ud.getHobby());
		udFromDB.setMusic(ud.getMusic());
		udFromDB.setMovie(ud.getMovie());
		udFromDB.setGame(ud.getGame());
		udFromDB.setComic(ud.getComic());
		udFromDB.setSport(ud.getSport());
		udFromDB.setBook(ud.getBook());
		userDetailDao.save(udFromDB);//根据表单参数修改信息后保存
		ud = udFromDB;//ud 指向 udFromDB用于显示页面
		return SUCCESS;
	}
	@Action(value="personal_info_cancel",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "personal_info_show.ftl")})
	public String personalInfoCancel(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return SUCCESS;
	}
	/**
	 * 联系方式
	 */
	@Action(value="contact_info_edit_input",
			results = {@Result(name = INPUT, type = ResultType.FREEMARKER, location = "contact_info_edit.ftl")})
	public String contactInfoEditInput(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return INPUT;
	}
	@Action(value="contact_info_save",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "contact_info_show.ftl")})
	public String contactInfoSave(){
		User user = getLoginUser();
		UserDetail udFromDB = userDetailDao.findOne("userName", user.getUserName());
		udFromDB.setQq(ud.getQq());
		udFromDB.setMsn(ud.getMsn());
		udFromDB.setMobilephone(ud.getMobilephone());
		udFromDB.setPersonalSite(ud.getPersonalSite());
		userDetailDao.save(udFromDB);//根据表单参数修改信息后保存
		ud = udFromDB;//ud 指向 udFromDB用于显示页面
		return SUCCESS;
	}
	@Action(value="contact_info_cancel",
			results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "contact_info_show.ftl")})
	public String contactInfoCancel(){
		User user = getLoginUser();
		ud = userDetailDao.findOne("userName", user.getUserName());
		return SUCCESS;
	}
	private UserDetail ud;
	private String otherUserName;
	
	@Inject
	private UserDetailDao userDetailDao;
	
	private static final long serialVersionUID = 1L;

	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}

	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}

	public UserDetail getUd() {
		return ud;
	}

	public void setUd(UserDetail ud) {
		this.ud = ud;
	}
	public String getOtherUserName() {
		return otherUserName;
	}
	public void setOtherUserName(String otherUserName) {
		this.otherUserName = otherUserName;
	}

}
